package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsSize;
import top.soulblack.purchasesale.service.GoodsSizeService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * create by 小川
 */
@Controller
@RequestMapping("/size")
public class GoodsSizeController {

    @Autowired
    private GoodsSizeService goodsSizeService;

    @ResponseBody
    @RequestMapping("/addSize")
    public Result<Boolean> addSize(GoodsSize goodsSize) {
        // 判断是否存在当前尺码
        if (goodsSizeService.existSize(goodsSize.getName())) {
            return Result.error(SIZE_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (goodsSize.getSort() > 100 || goodsSize.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        goodsSizeService.addSize(goodsSize);
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping("/editSize")
    public Result<Boolean> editSize(GoodsSize goodsSize) {
        System.out.println(goodsSize.getId());
        if (goodsSizeService.updateSize(goodsSize)) {
            return Result.success(true);
        }
        return Result.error(SIZE_EDIT_ERROR);
    }

    @ResponseBody
    @RequestMapping("/deleteSize")
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteSize(int sizeId) {
        System.out.println(sizeId);
        if (goodsSizeService.selectSize(sizeId)){
            return Result.error(SIZE_LIFE);
        }
        if (goodsSizeService.deleteSize(sizeId)) {
            return Result.success(true);
        }
        return Result.error(SIZE_DELETE);
    }
}
